#!/bin/bash
#功能函数
funcs=`which functions.sh`
if [ "$funcs" != "" ] ;then
    echo "funcs=$funcs"
    . $funcs
else
    funcs=./functions.sh
    if [ "$funcs" != "" ] ;then
	echo "funcs=$funcs"
	. $funcs
    else
	echo "./functions.sh not exist!"
	return
    fi
fi
default_project_name="TDXXXX,PDXXXX"
SVNCMD=`which svn.shc`

log_f_dir=$HOME/svnlog
log_f_V_all=$log_f_dir/${USER}-svn-log-v-all.txt
log_f_MAD=$log_f_dir/${USER}-svn-log-mad.txt
#生成信息到文件中
ReleaseNotesD=$HOME/releasenotes/
ReleaseNotesF=$ReleaseNotesD/ReleaseNotes.txt
MergeInfoF=$ReleaseNotesD/MergeInfos.txt
MergeNotesF=$ReleaseNotesD/MergeNotes.txt

function filter_versions()
{
  all_versions=$1
  xxx_versions=$2
  yyy_versions=
  for v in $all_versions
  do
    result=`echo "$xxx_versions" |grep $v -w`
    if [ -n "$result" ]; then
      yyy_versions="$yyy_versions $v"
    fi
  done
  echo "$yyy_versions"
}

function parse_svn_log_by_author()
{
  local f_in=$1
  local l_keyword=$2
  local f_out=$3
  local f_tmp=$log_f_dir/${USER}-svn-log-tmp3.txt
  cat $f_in | sed -n "/$l_keyword/,/----/p" >$f_out
}

function get_release_nostes()
{
  local f_in=$1
  local f_out=$2
  local project_name=$3
  local project_name2=$3
  local l_version=
  local l_keyword=
  if [ -n "$project_name" ]; then
    project_name="`echo $project_name |tr ',' ' '`"
  fi
  for n in $project_name
  do
    if [ -n "$l_keyword" ]; then
      l_keyword="$l_keyword|$n"
    else
      l_keyword=$n
    fi
  done
  l_version=`cat $f_in |grep ^r |awk '{print $1}'|tr -d 'r'`
  for v in $l_version 
  do 
    if [ -n "$l_keyword" ]; then
      if [ "$default_project_name" = "$project_name2" ]; then
        cat $f_in | sed -n "/r$v/,/----/p" >> $f_out
      else
        result=`cat $f_in | sed -n "/r$v/,/----/p" | grep -E "$l_keyword" `
        if [ -n "$result" ]; then
          cat $f_in | sed -n "/r$v/,/----/p" >> $f_out
        fi
      fi
    else
      cat $f_in | sed -n "/r$v/,/----/p" >> $f_out
    fi
  done
}

#脚本入口
while test $# != 0
do
    case $1 in
	--username)
	    shift
	    username=$1
	    ;; 
	--password)
	    shift
	    password=$1
	    ;; 

	-r)
	    shift
	    version=$1
	    ;; 
        -t)
	    shift
	    date=$1
	    ;; 
        -u)
	    shift
	    author=$1
	    ;; 
   	-m)
	    shift
	    mode=$1
	    ;;
   	-h)
	    shift
	    g_help=$1
	    ;;
   	-p)
	    shift
	    project_name=$1
	    ;;
	--work-copy)
	    shift
	    work_copy=$1
	    ;;
	--by-time)
	    shift
	    by_mode=$1
	    ;; 
  
	-by)
	    shift
	    by_mode=$1
	    ;; 
	
	--url)
	    shift
	    url=$1
	    ;;   
        *)
	   work_copy=$1
	   ;;
       esac
       shift
done
if [ -z "$work_copy" ]; then
  work_copy=`pwd`
fi

if [ "$mode" = "auto" ]; then
  start_v=800
  end_v=850
  project_name=PD1203
else
  if test -f $work_copy/.version; then
    start_v=`cat .version`
  else
    start_v=`sh_get_svn_committed_no_of_copy $work_copy`
  fi
  end_v=`sh_get_svn_head_no_of_copy $work_copy`
  version=`question  "请指定版本区间"  2 $start_v:$end_v `
  svn-log.sh -r $version $work_copy
  #指定项目名称
  if [ -z "$project_name" ]; then
    project_name=`question  "请指定项目名称"  2 "$default_project_name"`
  fi
fi

############
all_authors=`cat $log_f_MAD |awk '{print $1}' |sort |uniq`
all_versions=`cat $log_f_MAD  |awk '{print $2}' |sort |uniq`
all_files=`cat $log_f_MAD  |awk '{print $5}' |sort |uniq`


merge_versions="817 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835"
merge_versions=`question  "输入已经合并的版本:"  2 "$merge_versions"`
for v in $all_versions
do
  result=`echo $merge_versions|grep $v -w`
  if [ -z "$result" ]; then
    unmerge_versions="$unmerge_versions $v"
  fi 
done

rm -rf $ReleaseNotesD
mkdir $ReleaseNotesD/authors/ -pv

#拷贝ReleaseNotes文件
rm $ReleaseNotesF -rf; touch $ReleaseNotesF
get_release_nostes  $log_f_V_all $ReleaseNotesF "$project_name"
#处理每个提交者的信息
for a in $all_authors
do
author_versions=`cat $log_f_MAD |grep $a |awk '{print $1 " " $2}' |sort |uniq|awk '{print $2}'|tr '\n' ' '`
author_merge_versions=`filter_versions "$author_versions" "$merge_versions"`
author_unmerge_versions=`filter_versions "$author_versions" "$unmerge_versions"`
##输出到屏幕
echoTextBlue  "提交作者: $a --- authors/$a.txt 该作者log汇总"
echoTextCyan  "所有版本：$author_versions"
echoTextGreen "已合并:  $author_merge_versions"
echoTextRed   "未合并:  $author_unmerge_versions"
#输出到文件
echo  "提交作者: $a --- authors/$a.txt 该作者log汇总">>$MergeInfoF
echo  "所有版本：$author_versions">>$MergeInfoF
echo  "已合并:  $author_merge_versions">>$MergeInfoF
echo  "未合并:  $author_unmerge_versions">>$MergeInfoF
if [ -n "$author_unmerge_versions" ]; then
echo  "$a 未合并的版本:">>$MergeNotesF
echo  "$author_unmerge_versions">>$MergeNotesF
fi
parse_svn_log_by_author $log_f_V_all  $a $ReleaseNotesD/authors/$a.log
done
echo "合并的版本:$merge_versions"
echo "未合并版本:$unmerge_versions"


echoTextBlue  "分类保存svn修改记录:"
echoTextGreen "
$ReleaseNotesF
$MergeInfoF
$MergeNotesF
$ReleaseNotesD/authors
"

